﻿@using System.Web.Script.Serialization
@using Stamp_For_Africa_Version_2.Models
@using Stamp_For_Africa_Version_2.Repositories
@using Stamp_For_Africa_Version_2.ViewModels
@model Stamp_For_Africa_Version_2.ViewModels.AlbumDetailsViewModel
<link href="../../css/redmond/Album.css" rel="stylesheet" type="text/css" />
<link href="../../Content/AlbumStyles.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/AlbumScripts.js" type="text/javascript"></script>

@Html.Hidden("CurrentSelectedPage")
<script type="text/javascript">
  $(document).ready(function() {
    $('#CurrentSelectedPage').val("0");
  });

  function AddPageToAlbum(albumId) {
    $.ajax(
			{
			  type: 'POST',
			  url: '/MyAlbums/AddPageToAlbum',
			  data: { albumId: albumId },
			  dataType: "json",

			  success: function (data, response) {


			  }
			});

    return true;
  }
  
  function SavePageOrder(albumId, pageNumber, order) {      
    $.ajax(
			{
			  type: 'POST',
			  url: '/MyAlbums/SavePageOrderOfAlbum',
			  data: { albumId: albumId, pageNumber: pageNumber, order: order },
			  dataType: "json",

			  success: function (data, response) {


			  }
			});
    
  }
  
  function AcceptLayoutOfAlbum(albumId) {

    var pageCount = @Model.Albun.PageCount ;

    var i = 0;
    for(i; i < pageCount; i++) {

      var qTag = '#tabs-' + (i + 1);
      var stampOrder = '';
      $(qTag).find('li').each(function (index) {
        var sId = $(this).find('p').attr('stampId');
        stampOrder = stampOrder + sId + '|';
        
      });
      if (stampOrder != '')       //Why waste a call to the controller?
        SavePageOrder(albumId, i, stampOrder);
    }
    
    return false;
  }
  
</script>

@{
  ViewBag.Title = "Album: " + Model.Albun.Title;
}

<h2>@Model.Albun.Title details</h2>
<fieldset class="roundedpanel">
	<legend>Album</legend>
	<div class="display-label">
		Title</div>
	<div class="display-field">
		@Html.DisplayFor(model => model.Albun.Title)
	</div>
	<div class="display-label">
		Description</div>
	<div class="display-field">
		@Html.DisplayFor(model => model.Albun.Description)
	</div>
	<div class="display-label">
		Showcased</div>
	<div class="display-field">
		@Html.DisplayFor(model => model.Albun.Showcased)
	</div>
</fieldset>
<p>
	@Html.ActionLink("Edit", "Edit", new { id = Model.Albun.Id }) |
	@Html.ActionLink("Back to List", "Index")
</p>


<fieldset class="roundedpanel">
<button onclick="return AddPageToAlbum('@Model.Albun.Id')">Add a page to this album</button>
<button onclick="return AcceptLayoutOfAlbum('@Model.Albun.Id')">Accept the layout of this album</button>
<div id="album">
  <ul>
  @{
    for (int i = 0; i < Model.Albun.PageCount; i++)
    {
      int pageNum = i + 1;


        var sPage = "Page " + pageNum;
        <li>
          @Html.ActionLink(sPage, "GetStampsForPage", new {albumId = Model.Albun.Id, pageNumber = i})
        </li>
      
    }
  }    
  </ul>
  


</div>

<div id="collection">
  <ul>
    <li><a href="#Mint">Mint</a></li>
  </ul>


  <div id="Mint" class="albumPages ui-widget ui-helper-clearfix" >
	  <ul id="MintList" class="albumList ui-helper-reset ui-helper-clearfix" pagenumber="-1">  
		  @{
	
        AlbumPageViewModel stampCollection = new AlbumPageViewModel();
        stampCollection.PageNumber = 0;

        stampCollection.Album = Model.Albun;
        

        foreach (var collection in Model.MyMintCollection)
        {
          AlbumRepository albumRepository = new AlbumRepository();
          
          MembershipUser user = Membership.GetUser();
          Guid userId = Guid.Empty;
          if (user != null)
          {
            userId = (Guid) user.ProviderUserKey;
          }

					int remaining = albumRepository.GetRemainingAssignableQuantityForStamp(userId, collection.StampId, Collection.StampState.MintNeverHinged);
          if (remaining > 0)
          {
            @Html.Partial("_AlbumEntry", new AlbumEntryViewModel {AlbumId = stampCollection.Album.Id.ToString(), Origin = "collection", Collection = collection, Remaining = remaining})
          }
        }
          
		  }

	  </ul>  
  </div>
</div>

</fieldset>
